package com.ruoyi.service.jpjs.excellence.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.domain.jpjs.common.excellence.Excellence;
import com.ruoyi.domain.jpjs.common.excellence.ExcellenceFile;
import com.ruoyi.domain.jpjs.common.excellence.ExcellenctDetail;
import com.ruoyi.mapper.jpjs.excellence.ExcellenceMapper;
import com.ruoyi.service.jpjs.excellence.ExcellenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author C.fw
 * @explain 混凝土报表业务层处理
 * @since 2020/8/15 15:43
 */
@Service
public class ExcellenceServiceImpl implements ExcellenceService {

    @Override
    public int insertExcellenceFile(ExcellenceFile excellenceFile) {
        return excellenceMapper.insertExcellenceFile(excellenceFile);
    }

    @Autowired
    private ExcellenceMapper excellenceMapper;

    @Override
    public List<ExcellenceFile> selectOfficeFileList(Map<String, Object> map) {
        return excellenceMapper.selectOfficeFileList(map);
    }

    @Override
    public int deleteOfficeFileById(String file) {
        String[] fileId = Convert.toStrArray(file);
        return excellenceMapper.deleteOfficeFileById(fileId);
    }

    @Override
    public String importExcellence(List<Excellence> excellenceList, Boolean updateSupport) {
        if (StringUtils.isNull(excellenceList) || excellenceList.size() == 0)
        {
            throw new BusinessException("导入创优创标计划数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Excellence excellence : excellenceList)
        {
            try
            {
                // 验证是否存在这个用户
                List<Excellence> list = excellenceMapper.selectOfficeByProgramName(excellence);
                if (StringUtils.isEmpty(list))
                {
                    excellenceMapper.insertOffice(excellence);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目 " + excellence.getProgramName() + " 导入成功");
                }
//                else if (isUpdateSupport)
//                {
//                    user.setUpdateBy(operName);
//                    this.updateUser(user);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 更新成功");
//                }
//                else
//                {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
//                }
            }
            catch (Exception e)
            {
                failureNum++;
//                String msg = "<br/>" + failureNum + "、账号 " + user.getLoginName() + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<Excellence> selectOfficeByProgramName(Excellence excellence) {
        return excellenceMapper.selectOfficeByProgramName(excellence);
    }

    @Override
    public int insertExcellenceBatches(List<Excellence> excellence) {
        return excellenceMapper.insertExcellenceBatches(excellence);
    }

    @Override
    public int insertExcellenceDetail(ExcellenctDetail excellenctDetail) {
        return excellenceMapper.insertExcellenceDetail(excellenctDetail);
    }

    @Override
    public int updatePathById(Long detailId) {
        return excellenceMapper.updatePathById(detailId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteFilePathById(Long detailId) {
        Map<String, Object> map = new HashMap<>();
        map.put("detailId", detailId);
        List<ExcellenctDetail> excellenctdetail = excellenceMapper.selectExcellenceDetailList(map);
        return FileUtils.deleteFile(excellenctdetail.get(0).getPhysicalPath());
    }

    @Override
    public int updateExcellenceInfo(Excellence excellence) {
        return excellenceMapper.updateExcellenceInfo(excellence);
    }

    @Override
    public int deleteDetailById(String detail) {
        String[] detailId = Convert.toStrArray(detail);
        return excellenceMapper.deleteDetailById(detailId);
    }

    @Override
    public int updateExcellenceDetailById(ExcellenctDetail excellenctDetail) {
        return excellenceMapper.updateExcellenceDetailById(excellenctDetail);
    }

    @Override
    public int deleteExcellenceById(String excellence) {
        String[] excellenceId = Convert.toStrArray(excellence);
        return excellenceMapper.deleteExcellenceById(excellenceId);
    }

    @Override
    public List<ExcellenctDetail> selectExcellenceDetailList(Map<String, Object> map) {
        return excellenceMapper.selectExcellenceDetailList(map);
    }

    @Override
    public List<Excellence> getExcellenceList(Map<String, Object> map) {
        return excellenceMapper.getExcellenceList(map);
    }
}
